import axios from 'axios';
import CryptoJS from 'crypto-js';
import { API_BASE_URL } from '../config/config';

export async function login() {
  try {
    // 临时禁用加密进行测试
    const response = await axios.post(`${API_BASE_URL}/login`, {
      username: 'admin',
      password: 'admin123'
    });

    if (response.data.code === 200) {
      return response.data.token; // 假设返回的 token 不需要解密
    } else {
      throw new Error('Login failed');
    }
  } catch (error) {
    console.error('Error logging in:', error);
    throw error;
  }
}

export async function fetchArticles(token: string) {
  try {
    const response = await axios.get(`${API_BASE_URL}/blog/blog/list`, {
      params: {
        pageNum: 1,
        pageSize: 10
      },
      headers: {
        'Authorization': token
      }
    });
    if (response.data.code === 200) {
      return response.data.rows;
    } else {
      throw new Error('Failed to fetch articles');
    }
  } catch (error) {
    console.error('Error fetching articles:', error);
    throw error;
  }
}
